//
//  AppDelegate.swift
//  UAKA
//
//  Created by 王剑鹏 on 2020/4/26.
//  Copyright © 2020 waing. All rights reserved.
//

//国际化配置

import Foundation

class AppSettings: NSObject {

    fileprivate static let kSharedSettingsKey = "DefaultUserSettings"

    static let shared: AppSettings = {
        let appSettings: AppSettings
        if let savedData = UserDefaults.standard.object(forKey: AppSettings.kSharedSettingsKey) as? Data,
            let defaultSettings = NSKeyedUnarchiver.unarchiveObject(with: savedData) as? AppSettings {
            appSettings = defaultSettings
        } else {
            appSettings = AppSettings()
        }

        return appSettings
    }()

    static func saveSharedInstance() {
        let data = NSKeyedArchiver.archivedData(withRootObject: AppSettings.shared)
        UserDefaults.standard.set(data, forKey: AppSettings.kSharedSettingsKey)
    }

    enum Language: String {
        case Chinese = "zh-Hans"
        case English = "en"
        var code: String {
            return rawValue
        }
    }
    static func localeIsChinese() -> Bool {
        if let lang = Locale.preferredLanguages.first {
            if lang.hasPrefix("zh") {
                return true
            } else {
                return false
            }
        } else {
            return false
        }

    }

    var language: Language
    override init() {
        // 第一次初始语言, 看手机是什么语言
        //language = AppSettings.localeIsChinese() ? .Chinese : .English
        language = .Chinese
        super.init()
    }

}
private var bundleByLanguageCode: [String: Foundation.Bundle] = [:]
extension AppSettings.Language {
    var bundle: Foundation.Bundle? {
        if let bundle = bundleByLanguageCode[code] {
            return bundle
        } else {
            let mainBundle = Foundation.Bundle.main
            if let path = mainBundle.path(forResource: code, ofType: "lproj"),
                let bundle = Foundation.Bundle(path: path) {
                bundleByLanguageCode[code] = bundle
                return bundle
            } else {
                return nil
            }
        }
    }
}

class Bundle: Foundation.Bundle {
    override func localizedString(forKey key: String, value: String?, table tableName: String?) -> String {
        if let bundle = AppSettings.shared.language.bundle {
            return bundle.localizedString(forKey: key, value: value, table: tableName)
        } else {
            return super.localizedString(forKey: key, value: value, table: tableName)
        }
    }
}

//宏定义
extension String{
//    static let random_str_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    static func localKeyStr(key : String, comment: String) -> String{
        
        return NSLocalizedString(key, comment: comment)
    }
}



/**
https://www.jianshu.com/p/e5d5fac05b8c
https://github.com/PeiJueChen/ios-app-internationalization
 */
/**
 使用
 
 在 APPdelegate 中
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        //国际化配置
        object_setClass(Foundation.Bundle.main, Bundle.self)
        
        
        return true
    }

 
 //切换语言时,只要设置就可以:
 AppSettings.shared.language = .English //  .Chinese
 
 //在赋值处
 /// comment 是传参, 假如你的value 中需要外界来决定内容,可以用%@,然后comment传参.
 NSLocalizedString("key", comment: "")
 //或者
 l1.text = NSLocalizedString("label1", comment: "")

 */
/**
 对于story 中的国际化, 我们需要重设一下RootViewController
 func resetRootViewController() {
       if let appdelegate = UIApplication.shared.delegate {
           let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
           if let mainController = storyBoard.instantiateViewController(withIdentifier: "rootViewController") as? UINavigationController {
               appdelegate.window??.rootViewController = mainController
           }
       }
   }
 */
